package ovm.gui.views.news;

import com.vaadin.ui.*;
import ovm.entity.NewsEntity;
import ovm.gui.views.AbstractView;
import ovm.management.news.NewsManager;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import static ovm.application.MainApplication.*;

/**
 * Created by IntelliJ IDEA.
 * User: Jokser
 * Date: 07.06.12
 * Time: 18:35
 * To change this template use File | Settings | File Templates.
 */

public class NewsView extends AbstractView {
    
    protected VerticalLayout newsLayout;
    
    public NewsView() {
        super();
        setCaption("Новости и оповещения");
        ArrayList<NewsEntity> list = NewsManager.getAllNews();
        newsLayout = new VerticalLayout();
        for (final NewsEntity entity : list) {
            HorizontalLayout singleLayout = new HorizontalLayout();

            Date messageDate = entity.getnDate();
            DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy  HH:mm");

            Label dateLabel = new Label(dateFormat.format(messageDate));

            Button showButton = new Button(entity.getHeader() + ". Подробнее >>", new Button.ClickListener() {
                @Override
                public void buttonClick(Button.ClickEvent event) {
                    navigator.setMainView(new SingleNewsView(entity));
                }
            });
            showButton.setStyleName("link");

            singleLayout.setWidth("500px");

            singleLayout.addComponent(dateLabel);
            singleLayout.setExpandRatio(dateLabel, 1);
            singleLayout.addComponent(showButton);
            singleLayout.setExpandRatio(showButton, 1.5f);

            newsLayout.addComponent(singleLayout);
        }

        addComponent(newsLayout);

    }

}
